package com.bian.Userdao;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class Handler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

                //存在session，不拦截，不存在就拦截
                if (request.getSession().getAttribute("user")!=null){
                    return true;
                }else {
                    request.setAttribute("msg","请登录后再试！");
                    request.getRequestDispatcher("/").forward(request,response);

                }

                //返回值为false，表示拦截请求，返回值为true表示不拦截，放行请求
                return false;
            }

            //下面两个方法一般用于做日志
            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            }
        }




